<?php
require_once('common.inc.php');
require_once(_INC_PATH.'database.php');
require_once(_INC_PATH.'captcha.inc');
require_once(_INC_PATH.'page.inc.php');

if($_REQUEST['search_name'] == '输入企业名称或地址') unset($_REQUEST['search_name']);
//搜索关键词分词
$data = array();
$keyNum = 0;
$sql_cond = "";
$db = new database();

if(isset($_REQUEST['search_name'])&&$_REQUEST['search_name']){
	$keyArray = explode(" ",strtolower($_REQUEST['search_name']));	//关键词数组
	$keyNum = count($keyArray);
}
$sql_count = "select count(*) as total from tbl_corporation tc left join tbl_statistic ts on ts.cor_id=tc.id where del_flg='0'";
$sql_header = "select *
			from tbl_corporation tc left join tbl_statistic ts on ts.cor_id=tc.id where del_flg='0'";
if(isset($_REQUEST['search_name'])&&$_REQUEST['search_name']!=""){
	$sql_cond = " and (name like '%".mysql_escape_string($_REQUEST['search_name'])."%' 
				 or address like '%".mysql_escape_string($_REQUEST['search_name'])."%')";
}
$total = $db->loadResult($sql_count.$sql_cond);
if($total > 0){
	$data = getSearchRow($total,$sql_header,$sql_cond);
}

//如果like搜索不到,则按空格自动分词
if(count($data)<=0&&isset($_REQUEST['search_name'])&&$_REQUEST['search_name']!=""&&$keyNum>1)
{

	$sql_cond = " and (1=2 ";
	foreach($keyArray as $key=>$value)
	{
		if($value=="")continue;
		$sql_cond.=" or name like '%".mysql_escape_string($value)."%' 
					  or address like '%".mysql_escape_string($value)."%'";
	}
	$sql_cond2 = ")";
	$total = $db->loadResult($sql_count.$sql_cond);
	if($total > 0){
		$data = getSearchRow($total,$sql_header,$sql_cond);
	}
}

//如果按空格自动分词还搜索不到,则自动分词
if(count($data) <= 0 && isset($_REQUEST['search_name']) && $_REQUEST['search_name']!="")
{
	$letter="...";
	$search_name=mb_chunk_split($_REQUEST['search_name'],3, $letter); // 六...一...    
	$arr_search_name=explode($letter,$search_name);
	$sql_cond = " and (1=2 ";
	foreach($arr_search_name as $key=>$value)
	{
		if($value == "")continue;
		$sql_cond .= " or name like '%".mysql_escape_string($value)."%' 
					  or address like '%".mysql_escape_string($value)."%'";
	}
	$sql_cond .= ")";
	$total = $db->loadResult($sql_count.$sql_cond);
	if($total > 0){
		$data = getSearchRow($total,$sql_header,$sql_cond);
	}
}
if(count($data) <= 0){
	$smarty->assign('error',CORPORATIONNOTEXISTS);
	$addRow = array();
	$addRow['searchContent'] = $_REQUEST['search_name'];
	$addRow['ip'] = getIP();
	$addRow['create_time'] = date("Y-m-d H:i:s");
	$addRow['update_time'] = date("Y-m-d H:i:s");
	$addRow['del_flg'] = '0';
	$db->insert($addRow,'tbl_noresult');
}
if(count($data) > 0){
	foreach($data as $key=>$value)
	{
		$imgPath = findImg($value['id']);
		if($imgPath == ""){
			$imgPath = "/images/noimage.jpg";
		}
		$data[$key]['imgPath'] = $imgPath;
		$data[$key]['description'] = mb_substr($value['description'], 0 ,50, "utf-8")."...";
	}
}
$smarty->assign('result',$data);
/*企业排行**************************/
$sql = "select tc.* from tbl_corporation tc left join tbl_statistic ts on ts.cor_id=tc.id where tc.del_flg='0' order by ts.visit_total desc limit 10";
$db->setQuery($sql);
$rank_rows = $db->loadList();
$smarty->assign("rank_rows",$rank_rows);
/*企业排行 end**************************/

/*推荐企业**************************/
$sql = "select * from tbl_recom where del_flg='0' and type='qytj' limit 10";
$db->setQuery($sql);
$recom_rows = $db->loadList();
$smarty->assign("recom_rows",$recom_rows);
/*推荐企业 end**************************/
Display('corporation_list.tpl');

function getSearchRow($total,$sql_header,$sql_cond)
{
	global $smarty,$db;
	$curPage = @intval($_REQUEST['p']);
	if($curPage < 1) $curPage = 1;
	$showNum = _FORVIEWNUM;
	$pageLink = "/search.php?search_name=".rawurlencode($_REQUEST['search_name']);
	$page = new PageClass($total, $curPage, $showNum, $pageLink);
	$limitStart = $page->limitStart;
	$smarty->assign("pageList",$page->pageList);
	$sql_limit = " limit $limitStart,$showNum";
	$sql=$sql_header.$sql_cond.$sql_limit;
	//echo $sql;die;
	$db->setQuery($sql);
	return $db->loadList();
}
?>